/****************************************************************************
*  Arthea Server by R. Jennings (2007-2008)   http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses.      *
****************************************************************************/


using System;
using System.Reflection;

namespace Arthea.Connections.Colors
{
    /// <summary>
    /// Implements color values
    /// </summary>
    public struct ColorValue
    {
        /// <summary>
        /// A black foreground value
        /// </summary>
        public const int Black = 30;

        /// <summary>
        /// A black background value
        /// </summary>
        public const int BlackBG = 40;

        /// <summary>
        /// The blinking text attribute
        /// </summary>
        public const int Blink = 5;

        /// <summary>
        /// A blue foreground value
        /// </summary>
        public const int Blue = 34;

        /// <summary>
        /// A blue background value
        /// </summary>
        public const int BlueBG = 44;

        /// <summary>
        /// The bright attribute
        /// </summary>
        public const int Bright = 1;

        /// <summary>
        /// A Cyan foreground value
        /// </summary>
        public const int Cyan = 36;

        /// <summary>
        /// A cyan background value
        /// </summary>
        public const int CyanBG = 46;

        /// <summary>
        /// The dim attribute
        /// </summary>
        public const int Dim = 2;

        /// <summary>
        /// A green foreground value
        /// </summary>
        public const int Green = 32;

        /// <summary>
        /// A green background value
        /// </summary>
        public const int GreenBG = 42;

        /// <summary>
        /// The hidden attribute
        /// </summary>
        public const int Hidden = 8;

        /// <summary>
        /// A magenta foreground value
        /// </summary>
        public const int Magenta = 35;

        /// <summary>
        /// A magenta background value
        /// </summary>
        public const int MagentaBG = 45;

        /// <summary>
        /// Used for value saving
        /// </summary>
        public const int Mod = 10;

        /// <summary>
        /// No attribute
        /// </summary>
        public const int None = 0;

        /// <summary>
        /// Signals generation of a random value
        /// </summary>
        public const int Random = 9;

        /// <summary>
        /// A red foreground value
        /// </summary>
        public const int Red = 31;

        /// <summary>
        /// A red background value
        /// </summary>
        public const int RedBG = 41;

        /// <summary>
        /// The reverse attribute
        /// </summary>
        public const int Reverse = 7;

        /// <summary>
        /// The underscore attribute
        /// </summary>
        public const int Underscore = 4;

        /// <summary>
        /// A white foreground value
        /// </summary>
        public const int White = 37;

        /// <summary>
        /// A white background value
        /// </summary>
        public const int WhiteBG = 47;

        /// <summary>
        /// A yellow foreground value
        /// </summary>
        public const int Yellow = 33;

        /// <summary>
        /// A yellow background value
        /// </summary>
        public const int YellowBG = 43;

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string GetName(int value)
        {
            foreach (FieldInfo fi in typeof (ColorValue).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                if ((int) fi.GetValue(null) == value)
                    return fi.Name;
            }

            throw new Exception("Invalid color value");
        }

        /// <summary>
        /// Parses the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static int Parse(string name)
        {
            foreach (FieldInfo fi in typeof (ColorValue).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                if (fi.Name == name)
                    return (int) fi.GetValue(null);
            }

            throw new Exception("Invalid color value name");
        }
    }
}